package com.itheima.d2_reflect3;

import java.io.FileOutputStream;
import java.io.FilterOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;

public class SaveObjectFramework {
    public static void saveObject(Object obj)throws Exception{
        FileOutputStream fos = new FileOutputStream("C:\\Program Files\\Java\\code\\javaseadvrepro\\aaa\\a.txt",true);
        PrintStream ps = new PrintStream(fos);
        //1.获取字节码对象
        Class c = obj.getClass();
        //1.1获取没有包名的简单名字
        String simpleName = c.getSimpleName();
        ps.println("=========="+simpleName+"==========");
        //2.获取属性值
        Field[] Fields = c.getDeclaredFields();
        //3.遍历属性
        for (Field field : Fields) {
            //4.1获取属性的名字
            String fieldName = field.getName();
            //4.2获取属性的值
            field.setAccessible(true);//暴力反射
            Object fieldValue = field.get(obj);
            //5.打印到文件中
            ps.println(fieldName+"=="+fieldValue);
        }

        ps.close();
    }
}
